<template>
  <div class="request-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form inline @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model="selectId"
              class="summary-item"
              placeholder="选择开票类型"
              clearable
              filterable
            >
              <el-option
                v-for="subject in subjects"
                :key="subject.id"
                :value="subject.id"
                :label="subject.subject_name"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.status"
              placeholder="选择开票状态"
              clearable
            >
              <el-option value="待审核" />
              <el-option value="已驳回" />
              <el-option value="开票中" />
              <el-option value="已开票" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="search">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list">
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        type="index"
        width="95"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="支付类型" >
        <template #default="{ row }">
          <div> {{row.type === 2?'代开业务':'服务费/专票业务'}}</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="subject_name"
        label="科目名称"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="total_money"
        label="开票金额"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="create_time"
        label="申请时间"
      />
      <el-table-column align="center" label="开票状态">
        <template #default="{ row }">
          <span :class="getStatusClass(row.status)" @click="showReason(row)">{{row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="155">
        <template #default="{ row }">
          <!-- <el-button type="text" @click="showDetail(row.id)">详情</el-button> -->
          <el-button
            v-if="row.status === '已开票'"
            type="text"
            @click="showInvoice(row)"
          >
            发票信息
          </el-button>
          <el-button type="text" @click="showOrders(row.id)">
            查看关联订单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog title="发票照片" :visible.sync="invoiceVisible" width="500px">
      <el-form label-width="50px">
        <!--<el-image
          v-if="invoiceRow.bill_img"
          class="invoice-img"
          :src="invoiceRow.bill_img"
          :preview-src-list="[invoiceRow.bill_img]"
          :z-index="5000"
        />-->
        <div class="invoice-box">
        <div class="invoice-ul" v-for="(item,index) in invoiceRow.imgs" :key="index">
          <el-image
            v-if="item"
            class="invoice-img"
            style="width: 200px; height: 150px"
            :src="item"
            :preview-src-list="[item]"
            :z-index="5000"
          />
        </div>
        </div>
        <el-form-item label="物流">
          {{ invoiceRow.express_company }}
        </el-form-item>
        <el-form-item label="单号">
          {{ invoiceRow.express_number }}
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="invoiceVisible2" width="500px">
      <el-form label-width="120px">
        <el-form-item label="发票照片：">
          <div class="invoice-ul" v-for="(item,index) in invoiceRow.imgs" :key="index">
            <el-image
              v-if="item"
              class="invoice-img"
              style="width: 200px; height: 150px"
              :src="item"
              :preview-src-list="[item]"
              :z-index="5000"
            />
          </div>
        </el-form-item>
        <el-form-item label="物流单据：">
          <el-image
            class="invoice-img"
            style="width: 200px; height: 150px"
            :src="invoiceRow.express_number"
            :preview-src-list="[invoiceRow.express_number]"
            :z-index="5000"
          />
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog
      title="开票申请"
      :visible.sync="detailVisible"
      width="500px"
      @opened="hideReasons"
    >
      <el-form label-width="120px">
        <el-form-item label="编号">{{ detail.id }}</el-form-item>
        <el-form-item label="价税合计金额">
          {{ detail.total_money }}
        </el-form-item>
        <el-form-item label="开票科目">
          {{ detail.subject_name }}
        </el-form-item>
        <el-form-item label="开票月份">
          {{ detail.billing_date }}
        </el-form-item>
        <el-form-item label="开票状态">
          {{ detail.status }}
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog
      title="关联订单"
      :visible.sync="ordersVisible"
      custom-class="dialog-orders"
      @opened="hideReasons"
    >
      <el-table :data="orders">
        <el-table-column
          label="序号"
          type="index"
          align="center"
          show-overflow-tooltip
          width="95"
        />
        <el-table-column
          label="项目名称"
          prop="activity_name"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="付款金额"
          prop="subtotal"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="服务费"
          prop="servicetotal"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="付款申请时间"
          prop="create_time"
          align="center"
          show-overflow-tooltip
          width="170"
        />
        <el-table-column
          label="付款完成时间"
          align="center"
          show-overflow-tooltip
          width="170"
        >
          <template #default="{ row }">
            {{ row.pay_time || '' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="95" align="center">
          <template #default="{ row }">
            <el-button type="text" @click="showChecks(row.id)">
              查看验收材料
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog title="验收材料" :visible.sync="checksVisible" width="1130px">
      <div v-if="checks.length" class="checks">
        <el-image
          v-for="(check, index) in checks"
          :key="index"
          :src="checks[index]"
          :preview-src-list="checks"
          :z-index="5000"
          class="check"
          lazy
        />
      </div>
      <div v-else>没有提交验收材料</div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getRequestList,
    getRejectReason,
    getDetail,
    updateInvoice,
    getOrders,
    getChecks,
  } from '@/api/invoice'

  export default {
    name: 'InvoiceRequest',

    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          page: 1,
          list_row: 10,
          status: '',
          type:0
        },
        invoiceVisible: false,
        invoiceVisible2: false,
        invoiceRow: {},
        detailVisible: false,
        detail: {
          id: '',
          total_money: '',
          subject_name: '',
          billing_date: '',
          status: '',
        },
        ordersVisible: false,
        orders: [],
        checksVisible: false,
        checks: [],
        selectId:null,
        subjects:[{
          id: 1,
          subject_name: "服务费/专票业务"
        },{
          id: 2,
          subject_name: "代开业务"
        }],
      }
    },

    created() {
      this.getList()
    },

    activated() {
      this.getList()
    },

    methods: {
      async getList() {
        this.listLoading = true
        const { data, total } = await getRequestList(this.queryForm)
        this.list = data
        this.total = total
        this.listLoading = false
      },
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getList()
      },
      search() {
        this.queryForm.type = this.selectId;
        this.queryForm.page = 1
        this.getList()
      },
      getStatusClass(status) {
        switch (status) {
          case '已驳回':
            return 'status-rejected'
          default:
            return ''
        }
      },
      showReason({reject_msg, id, status}) {
        if (status !== '已驳回') return
        getRejectReason({ billing_id: id }).then((res) => {
          this.$alert(res.reject || '未提供驳回理由', '驳回原因')
        })
      },
      hideReasons() {
        this.list.forEach((invoice) => {
          invoice.reasonVisible = false
        })
      },
      showDetail(id) {
        getDetail({ billing_id: id }).then((res) => {
          this.detail = res.billing
          this.detailVisible = true
        })
      },
      closeDetail() {
        this.detailVisible = false
      },
      showInvoice(row) {
        console.log(1234,row)
        this.invoiceRow = row
        if(row.type === 1){
          this.invoiceVisible = true
          this.invoiceRow.imgs = row.img;
        }else if(row.type === 2){
          this.invoiceVisible2 = true
          this.invoiceRow.imgs = row.img;
          this.invoiceRow.express_number = row.express_number;
        }

      },
      async showOrders(id) {
        const detail = await getDetail({ billing_id: id })
        const ids = detail.billing.payment_application_detail_id
        const orders = await getOrders({ payment_application_detail_id: ids })
        this.orders = orders
        this.ordersVisible = true
      },
      showChecks(id) {
        getChecks({ id: id }).then((res) => {
          this.checks = res
          this.checksVisible = true
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .status-rejected {
    color: #f56c6c;
    cursor: help;
    border-bottom: 1px dashed #dcdfe6;
  }

  ::v-deep .dialog-orders {
    width: 80%;

    @media (min-width: 1440px) {
      width: 60%;
    }
  }

  .checks {
    display: flex;
    flex-wrap: wrap;
    margin-right: -10px;
    margin-bottom: -10px;
  }

  .check {
    width: 100px;
    height: 100px;
    margin-right: 10px;
    margin-bottom: 10px;
  }
  .invoice-box{
    flex: 1;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    .invoice-ul{
      display: flex;
      .invoice-img {
        display: flex;
        width: 200px;
        height: 200px;
        margin-right: 20px;
        margin-bottom: 18px;
      }
    }

  }


</style>
